#include "interface.h"

#define true 1
#define false 0
void Menu(user_act* u,TcpC* c)
{
    while (1)
    {
        //system("clear");
        printf("\n");
        printf("-----------------------------------------\n");
        printf("----        欢迎来到本聊天室   ----\n");
        printf("----        1   登录               ----\n");
        printf("----        2   注册新账号                ----\n");
        printf("----        3   退出程序                ----\n");
        printf("-----------------------------------------\n");

        printf("请输入您的选择 : \n");
        scanf("%d", &u->choice);
        while (getchar() != '\n');

        switch (u->choice)
        {
        case 1:
            Log(u,c);
            return ;
            break;
        case 2:
            Register(u,c);
            break;
        case 3:
            exit(0);
        default:
            printf("input error command!\n");
            break;
        }
    }
    return;
}

void Log(user_act* u,TcpC* c)
{
    int choice = u->choice;
    while(1)
    {
        u->choice = choice ;
        printf("请输入聊天号：\n");
        scanf("%s", u->ID);
        while (getchar() != '\n');

        printf("请输入账号名称：\n");
        scanf("%s", u->Name);
        while (getchar() != '\n');
        
        printf("请输入密码：\n");
        scanf("%s", u->passwd);
        while (getchar() != '\n');
        memset(u->Scontent,0,sizeof(u->Scontent));
        printf("error!\n");
        
        TcpClientSend(c, &u[0],sizeof(user_act));
        printf("error2!\n");

        TCPClientRecv(c,&u[0],sizeof(user_act));
        printf("error8!\n");
      
        if(u->choice == 1)
        {
            printf("%s",u->Scontent);
            return ;
        }  
        else
        {
            printf("%s",u->Scontent);
        }  
        printf("end:%d\n",u->choice);

    }
}

void Register(user_act* u,TcpC* c)
{
    char passwd2[20];
    printf("请输入账号名称 ：\n");
    scanf("%s",u->Name);
    while (getchar() != '\n');
    while(1)
    {
        printf("请输入您的密码(密码由不超过二十位的数字和字母组合而成) ：\n");
        scanf("%s", u->passwd);
        while (getchar() != '\n');
        printf("请再次输入来确认密码 ：\n");
        scanf("%s", passwd2);
        while (getchar() != '\n');
        if(strcmp(u->passwd,passwd2) != 0)
        {
            memset(u->passwd,0,10);
            memset(passwd2,0,10);
            printf("两次输入的密码并不一致! \n");
            printf("请再试一次！ \n");
        }
        else
        {
            break;
        }
    }
                
    TcpClientSend(c, &u[0],sizeof(user_act));
    TCPClientRecv(c,&u[0],sizeof(user_act));
    printf("%s\n",u->Scontent);
  
}

user_act *InitUser()
{
    user_act* user = (user_act*)malloc(sizeof(user_act));
    if(user == NULL)
    {
        printf("user_act malloc error!\n");
        return NULL ;
    }
    InitDLList(&user->Friend);
    memset(user->ID,0,20);
    memset(user->Name,0,20);
    memset(user->passwd,0,20);
    memset(user->Ccontent,0,20);
    memset(user->Scontent,0,20);

    user->status = 1 ;
    user->choice = -1 ;
    
    return user;
}
